#include <cstdio>
#include <cstring>

int main( )
{
    int t, i, p;
    int f[ 26 ];
    f[ 'I' - 'A' ] = 1;
    f[ 'V' - 'A' ] = 5;
    f[ 'X' - 'A' ] = 10;
    f[ 'L' - 'A' ] = 50;
    f[ 'C' - 'A' ] = 100;
    f[ 'D' - 'A' ] = 500;
    f[ 'M' - 'A' ] = 1000;
    char str[ 10001 ];
    scanf("%d", &t);
    while ( t-- )
    {
        scanf("%s", str);
        p = f[ str[ 0 ] - 'A' ];
        for ( i = 1; i < strlen( str ); i++ )
        {
            if ( f[ str[ i ] - 'A' ] > f[ str[ i - 1 ] - 'A' ] )
                p -= f[ str[ i - 1 ] - 'A' ] * 2;
            p += f[ str[ i ] - 'A' ];
        }
        printf("%d\n", p);
    }
    return 0;
}
